package com.link.base.base.productplan.service;

import com.link.base.base.productplan.dao.mybatis.mapper.ProdPlanHeadMapper;
import com.link.base.base.productplan.dao.mybatis.mapper.ProdPlanMapper;
import com.link.base.base.productplan.model.ProdPlan;
import com.link.base.base.productplan.model.ProdPlanHead;
import com.link.base.basic.model.ListOfValue;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.KeyGenerateService;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments 产品计划信息
 */
@Service
public class ProdPlanHeadServiceImpl extends BasicServiceImpl<ProdPlanHead> implements ProdPlanHeadService {

    @Resource
    protected KeyGenerateService keyGenerateService;
    @Resource
    private ProdPlanHeadMapper prodPlanHeadMapper;
    @Resource
    private ProdPlanMapper prodPlanMapper;

    @Override
    public BasicMapper<ProdPlanHead> getBasicMapper() {
        return prodPlanHeadMapper;
    }

    @Override
    public void afterInsert(ProdPlanHead entity) throws Exception {
        int i = 1;
        /*通过值列表生成行集合*/
        List<ProdPlan> list = new ArrayList<>();
        List<ListOfValue> stageList = prodPlanHeadMapper.queryListOfValbyType("DEV_PLAN_STAGE");
        for (ListOfValue stage : stageList) {
            List<ListOfValue> workItemList = prodPlanHeadMapper.queryListOfValbyPId(stage.getId());
            for (ListOfValue workItem : workItemList) {
                List<ListOfValue> workImChildList = prodPlanHeadMapper.queryListOfValbyPId(workItem.getId());
                for (ListOfValue workImChild : workImChildList) {
                    ProdPlan p = new ProdPlan();
                    p.setHeadId(entity.getId());
                    p.setStage(stage.getVal());
                    p.setWorkItem(workItem.getVal());
                    p.setSubItem(workImChild.getVal());
                    p.setTechManagerId(entity.getTechManagerId());
                    p.setProdManagerId(entity.getProdManagerId());
                    p.setSysTarget(entity.getSysTarget());
                    List<ListOfValue> outputList = prodPlanHeadMapper.queryListOfValbyPId(workImChild.getId());
                    for (ListOfValue output : outputList) {
                        if (output != null) {
                            p.setOutput(output.getVal());
                        }
                    }
                    p.setSeq(i++);
                    list.add(p);
                }
            }
        }

        for (ProdPlan prodPlan : list) {
            prodPlan.setId(keyGenerateService.keyGenerate());
            prodPlanMapper.insert(prodPlan);
        }

    }


}
